package org.lora.webframework.common;

import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.apache.log4j.Logger;
import org.lora.core.common.CommonCoreServiceImpl;
import org.lora.core.mybatis.OrderByBean;
import org.lora.log4j.Log4jUtil;
import org.lora.util.StringUtil;
import org.lora.util.ValidateUtil;

/**
 * <p>
 * Title:FrameWorkBaseService
 * </p>
 * <p>
 * Desription:Web框架Service基类
 * </p>
 * Create Time:2016年7月7日 下午7:16:55
 * 
 * @author Bladnir@outlook.com
 *         <p>
 *         History:
 *         </p>
 *         2016年7月7日 Bladnir Create</br>
 */
public class FrameWorkBaseService extends CommonCoreServiceImpl {

	protected Logger businessLog = Log4jUtil.getBusinessLogger();
	
	/**
	 * 获取排序字符串
	 * 
	 * @param orderByList
	 * @return
	 */
	protected String getOrderByClause(List<OrderByBean> orderByList) {

		String orderByClause = StringUtil.BLANK;

		if (ValidateUtil.isNotEmpty(orderByList)) {
			StringBuilder orderByClauseBuffer = new StringBuilder();

			for (OrderByBean orderBy : orderByList) {
				orderByClauseBuffer.append(orderBy.getColumName()).append(StringUtil.SPACE).append(orderBy.getOrderName().getStr())
						.append(",");
			}
			orderByClause = orderByClauseBuffer.toString();
			orderByClause = orderByClause.substring(0, orderByClause.length() - 1);
		}

		return orderByClause;
	}

	/**
	 * 从Map中获取字符串
	 * 
	 * @param key
	 * @param map
	 * @return
	 */
	protected String getStringFromMap(String key, Map<String, Object> map) {

		if (StringUtil.isNull(key) || map == null || map.isEmpty()) {
			return null;
		}

		if (map.containsKey(key)) {
			Object value = map.get(key);
			if (StringUtil.isNotNull(value)) {
				return value.toString();
			} else {
				return null;
			}
		} else {
			return null;
		}

	}

	/**
	 * 生成主键ID
	 * 
	 * @param prefix
	 *            前缀
	 * @return
	 */
	protected String creatBeanId(String prefix) {
		StringBuilder sb = new StringBuilder();
		sb.append(prefix).append("-").append(UUID.randomUUID());

		return sb.toString().toUpperCase();
	}

}
